<?php

declare(strict_types=1);

namespace Modules\Pmcsignage\Http\Controllers;

use Catch\Base\CatchController as Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Modules\Base\Repository\MaterialRepository;
use Modules\Production\Models\ProductionOrder;
use Modules\Production\Models\ProductionOrderDetail;
use Modules\Production\Models\ProductionOrderStockUser;
use Modules\Production\Repository\ProductionOrderRepository;
use Modules\Production\Repository\ProductionOrderStockUserRepository;
use Modules\Production\Service\ProductionOrderService;
use Modules\Pmcsignage\Http\Repositories\TodayproductionReget;
use Modules\Production\Models\ProductionReportOrder;
use App\Enums\ProductionOrderBackStatus;
use App\Enums\ProductionOrderStatus;
use Modules\Pmcsignage\Models\ProductionTheweekplan;


class TodayproductionController extends Controller
{
    public function __construct(
        protected readonly ProductionOrder $model
    ){}


    /* 今日生产情况
     * @param Request $request
     * @return mixed
     */
    public function index(Request $request): mixed
    {
        $first_day_of_week = strtotime('this week Monday');
        $last_day_of_week = strtotime('this week Sunday');
        $str = date('Y-m-d',$first_day_of_week);
        $end = date('Y-m-d',$last_day_of_week);
        
        $data = TodayproductionReget::listtable($request->all());
        
       
           $Plantype = ProductionTheweekplan::$Plantype;
        foreach($Plantype as $k=>$v){
            $label[$v['value']] = $v['label'];
        }
    
        foreach($data as $k=>$v){
            $data[$k]['rateof'] = round($v['report_qty'] / $v['fqty'] * 100,2)."%";//数量再除总数，得出百分比 
            $datares = ProductionReportOrder::query()->select('production_report_order.docno','production_report_order.id')
            ->with('detailscheckOne')
            ->where('production_report_order.production_no',$v['fbillno'])->first();
            if(!empty($datares)){
                $data[$k]['inbound_qty'] = $datares['detailscheckOne']['inbound_qty'];
                $data[$k]['total_hours'] = $datares['detailscheckOne']['total_hours'];
            }else{
                $data[$k]['inbound_qty'] = '';
                $data[$k]['total_hours'] = '';
            }
            // (已领数量- 固定损耗)/ (分子/分母 ) * 1.05/
            $produ = ProductionOrderDetail::select(
                DB::raw('SUM(fpickedqty) as fpickedqty'),
                DB::raw('SUM(ffixscrapqty) as ffixscrapqty'),
                DB::raw('SUM(fnumerator) as fnumerator'),
                DB::raw('SUM(fdenominator) as fdenominator')
                )->where('production_order_id',$v['id'])
                ->groupby(['production_order_id'])->first();
          
                $back_number = 0.0000;
                if(!empty($produ)){
                    if($produ['ffixscrapqty'] > 0){
                        $kai = sprintf("%.4f",$produ['fpickedqty']/$produ['ffixscrapqty']);
                        $end = sprintf("%.4f",$produ['fnumerator']/$produ['fdenominator']);
                        $back_number = sprintf("%.4f",($kai/$end)* 1.05);
                    }
                }
                $data[$k]['back_number'] = $back_number;
                
                $plan = ProductionTheweekplan::select('plantype')->where('production_order_id',$v['id'])->first();
                if(!empty($plan)){
                   $data[$k]['plantype'] = isset($label[$plan['plantype']])?$label[$plan['plantype']]:""; 
                }else{
                    $data[$k]['plantype'] = "";
                }

                if($v['created_at'] >= $str || $v['created_at'] <= $end){
                    $data[$k]['unscheduledtime'] = $v['created_at'];
                }
                   
        }
        return $data;
        // return $this->model->getList();
    }



    /* 待备货-接口
    * @param Request $request
    * @return mixed
    */
    public function goodstock(Request $request)
    {
       
          $Plantype = ProductionTheweekplan::$Plantype;
        foreach($Plantype as $k=>$v){
            $label[$v['value']] = $v['label'];
        }
    
    
        foreach (ProductionOrderStatus::cases() as $case){
            $list[$case->value] =$case->label();
        }

        // foreach (ProductionOrderBackStatus::cases() as $case){
        //     $listBack[$case->value] =$case->label();
        // }
        // $data['back_status'] = ProductionOrderBackStatus::WAIT_BACK->value; //待备货

        $data = TodayproductionReget::listtable_goodstock($request->all());
        foreach($data as $k=>$v){
            $productionOrder = ProductionOrder::select('fbillno','fmaterialid','fmaterialname','fqty','fmoentrystatus','created_at','id')
            ->where('id',$v['production_order_id'])
            ->first();
            $plan = ProductionTheweekplan::select('plantype')->where('production_order_id',$v['production_order_id'])->first();
            if(!empty($plan)){
                $data[$k]['plantype'] = isset($label[$plan['plantype']])?$label[$plan['plantype']]:"";
            }else{
                $data[$k]['plantype'] = "";
            }
            
            // $data[$k]['back_status'] = isset($listBack[$v['back_status']])?$listBack[$v['back_status']]:"";
            $data[$k]['fmoentrystatus'] = isset($list[$productionOrder['fmoentrystatus']])?$list[$productionOrder['fmoentrystatus']]:"";
            $data[$k]['fbillno'] = $productionOrder['fbillno'];
            $data[$k]['fmaterialname'] = $productionOrder['fmaterialname'];
            $data[$k]['examine_time'] = $productionOrder['created_at'];
            $user_name = ProductionOrderStockUser::select('user_name')->where('production_order_id',$v['production_order_id'])->first();
            if(!empty($user_name)){
                $data[$k]['user_name'] = $user_name['user_name'];
            }else{
                $data[$k]['user_name'] = '';
            }
            $data[$k]['fnopickedqty'] = $v['fmustqty'] - $v['fpickedqty'];
            
        }
        return $data;
        
    }

   

}
